<?php
namespace WeChatPay\Request;

use function WeChatPay\FuncReturn;

/**
 * Request 基础类
 * Class BaseRequest
 * @package WeChatPay\Request
 */
abstract class AbstractBaseRequest
{
    /**
     * 接口请求地址
     * @var string
     */
    protected $apiUri = '';


    /**
     * 请求方式
     * @var string
     */
    protected $requestType = 'post';

    /**
     * 必填参数列表(除了配置表中的几个参数，其他的必填参数都在这里填写)
     * @var array
     */
    protected $mustAddParams = [];

    /**
     * 已经添加Body的参数
     * @var array
     */
    protected $alreadyAddBodyParams = [];

    /**
     * 已经添加Query的参数
     * @var array
     */
    protected $alreadyAddQueryParams = [];

    /**
     * 已经添加Path的参数
     * @var array
     */
    protected $alreadyAddPathParams = [];

    /**
     * 属性映射方法名称
     * @var array
     */
    protected $propertyMapSetFunction = [];

    /**
     * 添加Body参数记录
     * @param $paramName
     */
    protected function addBodyParams($paramName) {
        $this->alreadyAddBodyParams[] = $paramName;
    }

    /**
     * 添加Query参数记录
     * @param $paramName
     */
    protected function addQueryParams($paramName) {
        $this->alreadyAddQueryParams[] = $paramName;
    }

    /**
     * 添加Path参数记录
     * @param $paramName
     */
    protected function addPathParams($paramName) {
        $this->alreadyAddPathParams[] = $paramName;
    }

    /**
     * 获取接口请求地址
     * @return string
     */
    public function getApiUri() {
        return $this->apiUri;
    }

    /**
     * 获取请求方式
     * @return string
     */
    public function getRequestType() {
        return strtolower($this->requestType);
    }

    /**
     * 获取配置基础参数列表(当前接口用到的基础配置中的参数列表)
     * @return array
     */
    public function getBaseParams(): array {
        return ['mchid','appid','notify_url','refund_notify_url','wap_url','wap_name'];
    }

    /**
     * 设置多个参数
     * @param array $addParamsList 数组key为属性名称，value为需要设置的值。eg: ['attach'=>'附加数据']
     * @return void
     */
    public function setManyParams($addParamsList = []) :void {
        foreach ($addParamsList as $property => $value) {
            if (!array_key_exists($property, $this->propertyMapSetFunction)) {
                throw new \Exception("{$property} property not exits");
            }

            $this->{$this->propertyMapSetFunction[$property]}($value);
        }
    }

    /**
     * 获取请求参数列表
     * @return array
     */
    public function getRequestParams() {
        $alreadyAddParams = array_merge($this->alreadyAddBodyParams, $this->alreadyAddPathParams, $this->alreadyAddQueryParams);
        $needParamList = array_diff($this->mustAddParams, $alreadyAddParams);
        if ($needParamList) {
            return FuncReturn(WX_RETURN_CHECK_PARAM_FAIL, 'params: (' . implode(',', $needParamList) . ') is must.');
        }

        $alreadyAddParams = array_merge($this->alreadyAddBodyParams, $this->alreadyAddQueryParams);
        $data = [];
        foreach ($alreadyAddParams as $tempParam) {
            $data[$tempParam] = $this->{$tempParam};
        }

        return FuncReturn(WX_RETURN_SUCCESS, '', $data);
    }
}